<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="com.apache.uct.common.ToolsUtil" %>
<%
String download_url  = ToolsUtil.getInstance().getValueByKey("DOWNLOAD_URL");
%>
 
<!DOCTYPE html>
<html>
<head>
	<title>附件单个录入案例-附件上传</title>
	<%@ include file="/common/meta.jsp" %>
	<%@ include file="/common/import.jsp" %>
	<script type="text/javascript" src="${ctx}/js/zbxtools.js"></script>
	<script type="text/javascript" src="${ctx}/js/easyui/extendvalidate.js"></script>
	<script type="text/javascript">
	var upload_do ='${ctx}/attachment/store/uploadAttachment.action';//上传附件或修改附件信息
	   $(function(){
	      /*$('#uploadattaStoreAdd').show();
		  $('#uploadattaStoreUpd').hide();*/
		  
		$('#attaStore').form({
		    url:upload_do,
		    onSubmit:function(){
		    	
		    	var uploadP = $('#uploadP');
		    	if(uploadP.val() == null ||  $.trim(uploadP.val()).length == 0){
		    	 $.messager.alert('提交结果','请选择要上传的文件!', 'error',function(){});    		
		    	 return false;
		    	}
		    	var attachmentName = $('#attachmentName');
		    	if(attachmentName.val() == null || $.trim(attachmentName.val()).length == 0){
		    		$.messager.alert('提交结果','附件名称 不能为空!', 'error',function(){});  
		    		attachmentName.focus();
		    		return false;
		    	}
		    	if(attachmentName.val().length >100){
		    		$.messager.alert('提交结果','附件名称 不能超过100字!', 'error',function(){});  
		    		attachmentName.focus();
		    		return false;
		    	}
		    	
		    	return true;
		    },
		    success:function(data){
		    	var oTxt = jQuery.parseJSON(data);
				var mess = oTxt.msg;	
				var flag = oTxt.flag;
				if(oTxt.flag == 'T'){
					 $.messager.alert('提交结果',mess, 'info',function(){
				        	parent.loadAttas();
				        	parent.closeAdd();//关闭窗口
			        });
				}else{
					 $.messager.alert('提交结果',mess, 'error',function(){
				        	parent.loadAttas();
				        	parent.closeAdd();//关闭窗口
			        });
				}
				clearForm();
		    }
	   });
	   var attDesc = document.getElementById("attaDesc");
	   textLimitCheck(attDesc,1000);
	   });
	   
	   //修改按钮调用-提交
		function doUpdate(){
			updAttaInfo(upload_do);
	
		}
	   
	//修改附件	
	function updAttaInfo(doUrl){
	
		$('#attaStore').form('submit',{
		    url:doUrl,
		    onSubmit:function(){
		    	var attachmentName = $('#attachmentName');
		    	if(attachmentName.val() == null || $.trim(attachmentName.val()).length == 0){
		           $.messager.alert('提交结果','附件名称 不能为空!', 'error',function(){});  
		    		attachmentName.focus();
		    		return false;
		    	}
		    	if(attachmentName.val().length >100){
		    		$.messager.alert('提交结果','附件名称 不能超过100字!', 'error',function(){});  
		    		attachmentName.focus();
		    		return false;
		    	}
		    	
		    	return true;
		    },
		    success:function(data){
		    	var oTxt = jQuery.parseJSON(data);
				var mess = oTxt.msg;	
				var flag = oTxt.flag;
		        if(oTxt.flag == 'T'){
					 $.messager.alert('提交结果',mess, 'info',function(){
				        	parent.loadAttas();
				        	parent.closeAdd();//关闭窗口
			        });
				}else{
					 $.messager.alert('提交结果',mess, 'error',function(){
				        	parent.loadAttas();
				        	parent.closeAdd();//关闭窗口
			        });
				}
		        clearForm();
		    }
		});
	}
	   
	   
function setFileName(){
	var ffName =  $("#uploadP").val();
	var spos = ffName.lastIndexOf("\\");  
		spos++;
	var tfName = ffName.substring(spos,ffName.length); 
		spos = tfName.lastIndexOf("."); 
	var finalName=tfName.substring(0,spos);
	//alert(finalName);
	if(null == $("#attachmentName").val()||""==$("#attachmentName").val())$("#attachmentName").val(finalName);
	if(null == $("#attaDesc").val()||""==$("#attaDesc").val())$("#attaDesc").val(finalName);
	var attDesc = document.getElementById("attaDesc");
	textLimitCheck(attDesc,1000);
}

 //---------------------------描述内容，字数验证--------------------------	
String.prototype.getBytes = function() {     
    var cArr = this.match(/[^\x00-\xff]/ig);     
    return this.length + (cArr == null ? 0 : cArr.length);     
}  
function textLimitCheck(thisArea, maxLength){  
    var len = thisArea.value.getBytes();  
    if (len > maxLength)  
    {  
        alert(maxLength + ' 个字限制. \r超出的将自动去除.');  
        var tempStr = "";  
        var areaStr = thisArea.value.split("");  
        var tempLen = 0;  
        for(var i=0,j=areaStr.length;i<j;i++){  
            tempLen += areaStr[i].getBytes();  
            if(tempLen<=maxLength){  
                tempStr += areaStr[i];  
            }                 
        }             
        thisArea.value = tempStr  
        thisArea.focus();  
        }  
        /*回写span的值，当前填写文字的数量*/  
        messageCount.innerText = thisArea.value.length;  
    }  
   //-----------------------------------------------------

	</script>
	
<style type="text/css">
	body{ font-size:14px;}
	input{ vertical-align:middle; margin:3px; padding:0}
	.txt{ height:22px; border:1px solid #cdcdcd; width:180px;}
	.btn{ background-color:#FFF; border:1px solid #CDCDCD;height:24px; width:70px;}
	.texta{ border:1px solid #cdcdcd; }
	 .labelinfo{
	  font-weight: bold;
	  color: #575765;
	  height: 16px;
	  line-height: 16px;
	}
</style>

    
</head>
<body>
<div id="atta_Store_form"  title="附件上传"  style="padding-left:10px;width: 500px;">
<br>
	<form id="attaStore" action="" method="post" enctype="multipart/form-data">
	    <input type="hidden" name="formToken" id="formToken" value="${formToken}"/>
	    <input type="hidden" name="sid" id="sid" value="${attaStore.sid}"/>
		<input type="hidden" name="objId" value="${attaStore.objId}"/>
		<input type="hidden" name="objType" value="${attaStore.objType}"/>
		
		<label>文　　件</label>		
	    <input type="file" name="upload" id="uploadP"  onchange="setFileName();" class="txt"  />  <br/> 
	    <label>附件名称</label>
	    <input type="text" name="attachmentName" id="attachmentName" class="txt" maxLength="20" value="${attaStore.attachmentName}" /> <br/> 
	    <label>附件描述</label>
	    <textarea name="attachmentDesc" id="attaDesc" cols="3 0" rows="5"  class="texta" onKeyUp="textLimitCheck(this, 1000);" >${attaStore.attachmentDesc}</textarea>
	   <br/> <span>(限1000个字符  已输入 <font color="#CC0000"><span id="messageCount">0</span></font> 个字)</span> 
	</form>
	<c:if test="${empty attaStore.sid}">
	<a href="#" class="easyui-linkbutton" id="uploadattaStoreAdd" onclick="$('#attaStore').submit();">文件上传</a>
    </c:if>
    <c:if test="${not empty attaStore.sid}">
	<a href="#" class="easyui-linkbutton" id="uploadattaStoreUpd" onclick="doUpdate();">修改文件信息</a>
	</c:if>
	<br>
</div>

</body>
</html>
